package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private DishService dishService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加菜品/套餐在购物车中
    @RequestMapping("add")
    private R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("接收到的数据为：{}", shoppingCart);

        //1.获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //2.判断是菜品还是套餐，设置查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //3,判断菜品/套餐是否已经添加过了，设置添加逻辑        查询到一条购物车数据
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
        //已经添加过了，就修改修改菜品份数
        if (cartServiceOne != null) {
            Integer number = shoppingCart.getNumber();
            cartServiceOne.setNumber(number + 1);
            //在设置到数据库
            boolean b = shoppingCartService.updateById(cartServiceOne);
            if (!b) {
                return R.error("添加套餐错误，请稍后再试");
            }
        } else {
            //没有添加过，就先直接添加
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            boolean b = shoppingCartService.save(shoppingCart);
            if (!b) {
                return R.error("添加套餐错误，请稍后再试");
            }
            cartServiceOne = shoppingCart;        //对象相同可以直接赋值
        }

        //返回购物车数据给前端
        return R.success(cartServiceOne);
    }

    /*
            清除购物车功能：
                需求：只要用户调用此功能就直接清除改用户下的购物车所有信息
     */
    @DeleteMapping("clean")
    public R<String> clean() {
        //调用放啊直接删除数据库中所有信息
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        boolean b = shoppingCartService.remove(lqw);
        return b ? R.success("删除成功") : R.error("删除失败");
    }

    //查询购物车中的所有属性
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        //根据用户id查询用户的购物车所有属性
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(lqw);
        log.info("查询到的购物车数据为：{}", list);
        return R.success(list);
    }

    //减少菜品/套餐的数量
    @PostMapping("sub")
    public R<ShoppingCart> update(@RequestBody ShoppingCart shoppingCart) {
        log.info("获取到前端要减少的属性为:{}", shoppingCart);

        //1.判断要减少的是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId() != null) {  //如果是菜品,在购物车中获取菜品对象
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            lqw.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
            ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);  //获取到购物车对象
            if (cartServiceOne == null) {
                return R.error("操作异常");
            }
            Integer number = cartServiceOne.getNumber();    //获取到购买数量,判断是否为0
            if (number == 0) {     //直接删除
                boolean remove = shoppingCartService.remove(lqw);
                if (!remove) {
                    return R.error("操作异常");
                }
            } else if (number > 0) {        //修改number
                cartServiceOne.setNumber(number - 1);
                //在数据库中改变
                boolean b = shoppingCartService.updateById(cartServiceOne);
                if (!b) {
                    return R.error("异常");
                }

                //如果当前购买数量减一为0 则直接删除数据；
                if ((number - 1) == 0) {
                    boolean remove = shoppingCartService.remove(lqw);
                    if (!remove) {
                        return R.error("操作有误");
                    }
                }

            } else {     //抛出异常
                return R.error("删除异常");
            }

            return R.success(cartServiceOne);

        }

        //如果是套餐
        LambdaQueryWrapper<ShoppingCart> slqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart cartServiceOne = shoppingCartService.getOne(slqw);  //获取到套餐在数据库中的值
        if (cartServiceOne == null) {
            return R.error("异常");
        }

        Integer number = cartServiceOne.getNumber();    //获取到购买数量,判断是否为0
        if (number == 0) {     //直接删除
            boolean remove = shoppingCartService.remove(lqw);
            if (!remove) {
                return R.error("操作异常");
            }
        } else if (number > 0) {        //修改number
            cartServiceOne.setNumber(number - 1);
            //在数据库中改变
            boolean b = shoppingCartService.updateById(cartServiceOne);
            if (!b) {
                return R.error("异常");
            }

            //如果当前购买数量减一为0 则直接删除数据；
            if ((number - 1) == 0) {
                boolean remove = shoppingCartService.remove(slqw);
                if (!remove) {
                    return R.error("操作有误");
                }
            }

        } else {     //抛出异常
            return R.error("删除异常");
        }

        return R.success(cartServiceOne);
    }

}
